export {};
// 对象类型的定义
let person: { name: string; age: number } = {
  name: 'bingo',
  age: 18,
};

person.name = 'xxxx';
// person.name = 123 // no
// person.age = false // no

let student: { name: string; age: number; todos: string[] } = {
  name: 'bingo',
  age: 18,
  todos: ['吃饭', '睡觉', '敲代码'],
};

// 追加一个函数
let student2: {
  name: string;
  age: number;
  todos: string[];
  code(code: string): string;
} = {
  name: 'bingo',
  age: 18,
  todos: ['吃饭', '睡觉', '敲代码'],
  // 函数
  code(code) {
    return code;
  },
};

student2.code('123');

// 个下面对比
let obj: {} = {};
obj = { a: 'a' };

let arr: [] = [];
// arr = [1,2] // 不行

// 定义 student 的类型
type Student = {
  name: string;
  age: number;
  todos: string[];
  code(code: string): string;
};

let student3: Student = {
  name: '',
  age: 0,
  todos: [],
  code: function (code: string): string {
    return code;
  },
};

// 对象可选参数类型定义
type Config = {
  url: string;
  method?: string;
};
let config: Config = {
  url: 'xxx',
  method: 'get',
};
